package com.roncoo.education.course.service.feign.biz;

import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.roncoo.education.course.feign.qo.UserCourseAppointmentQO;
import com.roncoo.education.course.service.dao.UserCourseAppointmentDao;
import com.roncoo.education.course.service.dao.impl.mapper.entity.UserCourseAppointment;
import com.roncoo.education.user.feign.interfaces.IFeignUser;
import com.roncoo.education.util.dingd.DingDUtil;
import com.roncoo.education.util.tools.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class FeignUserCourseAppointmentBiz {

    @Autowired
    private UserCourseAppointmentDao dao;

    @Autowired
    private IFeignUser iFeignUser;

    @Autowired
    private DingDUtil dingDUtil;

    public int save(UserCourseAppointmentQO qo) {
        UserCourseAppointment record = BeanUtil.copyProperties(qo, UserCourseAppointment.class);
        return dao.save(record);
    }

    /**
     * 发送消息
     * @param userNo 用户编号
     * @param message 要发送的消息
     * @return 1:成功；0:失败
     */
    public int sendMsg(Long userNo, String message) {
        String accessToken = dingDUtil.getAccessToken();
        OapiMessageCorpconversationAsyncsendV2Response oapiMessageCorpconversationAsyncsendV2Response = dingDUtil.sendMessage(iFeignUser.getByUserNo(userNo).getDdId(), message,accessToken);
        return !ObjectUtils.isEmpty(oapiMessageCorpconversationAsyncsendV2Response) && oapiMessageCorpconversationAsyncsendV2Response.getErrcode() == 0L ? 1 : 0;
    }

}
